<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ include file="/include/taglibs.jsp"%>
<html>
<head>
	<title>新增用户</title>
	<%@ include file="/include/openstyles.jsp"%>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/jquery.validate.min.js"></script>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/validate-methods.js"></script>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/jquery/jquery-form.js"></script>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/input.js"></script>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/js/lui.jiantingdiv.js"></script>
	<script type="text/javascript" src="${ctx}/assets/uc/js/common.js"></script>
	<script type="text/javascript" src="${ctx}/assets/admin/LingUI/datetimepicker/jquery.datetimepicker.js"></script>
	<link type="text/css" href="${ctx}/assets/admin/LingUI/datetimepicker/css/jquery.datetimepicker.css"/>
	<style>
		.help-block{
			margin: 0px!important;
		}
	</style>
</head>
<body>
<div class="row">
	<div class="col-xs-12">
		<div class="space-12"></div>
		<form class="form-horizontal" role="form" id="user_form">
			<div class="tabbable">
				<div class="profile-user-info profile-user-info-striped">
					<div class="profile-info-row">
						<div class="profile-info-name" >用户编号：</div>
						<div class="profile-info-value">
							<input type="text" id="username" name="username"
								   class="col-xs-10 col-sm-8 " placeholder="字母数字和下划线组成"  />
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >用户名称：</div>
						<div class="profile-info-value">
							<input type="text" id="name" name="name"
								   class="col-xs-10 col-sm-8" placeholder="汉字字母数字或下划线组成" />
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >性别：</div>
						<div class="profile-info-value">
							<select id="gender" name="gender"
									class="form-control col-xs-10 col-sm-8">
							</select>
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >身份证号码：</div>
						<div class="profile-info-value">
							<input type="text" id="identity" name="identity"
								   class="col-xs-10 col-sm-8" placeholder="请填写正确的身份证号" />
						</div>
					</div>

					<div class="profile-info-row">
						<div class="profile-info-name" >电子邮件：</div>
						<div class="profile-info-value">
							<input type="text" id="email" name="email"
								   class="col-xs-10 col-sm-8" placeholder="电子邮件" />
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >出生日期：</div>
						<div class="profile-info-value">
							<input id="birthday" name="birthday" type="text"
								   class="col-xs-10 col-sm-8">
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >固定电话：</div>
						<div class="profile-info-value">
							<input id="tel" name="tel" type="text"
								   class="col-xs-10 col-sm-8" placeholder="固定电话">
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >移动电话：</div>
						<div class="profile-info-value">
							<input id="mobile" name="mobile" type="text"
								   class="col-xs-10 col-sm-8" placeholder="移动电话">
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >联系地址：</div>
						<div class="profile-info-value">
							<input id="address" name="address" type="text"
								   class="col-xs-10 col-sm-8">
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >邮政编码：</div>
						<div class="profile-info-value">
							<input id="post" name="post" type="text"
								   class="col-xs-10 col-sm-8">
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >用户类型：</div>
						<div class="profile-info-value">
							<select id="userType" name="userType"
									class="form-control col-xs-10 col-sm-8">
							</select>
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >是否启用：</div>
						<div class="profile-info-value">
							<select id="enabled" name="enabled"
									class="form-control col-xs-10 col-sm-8">
							</select>
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >用户岗位：</div>
						<div class="profile-info-value">
							<select id="position" name="position"
									class="form-control col-xs-10 col-sm-8">
							</select>
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >排序字段：</div>
						<div class="profile-info-value">
							<input type="text" id="order" name="order"
								   class="col-xs-10 col-sm-8" placeholder="数字组成" />
						</div>
					</div>
					<div class="profile-info-row">
						<div class="profile-info-name" >描述信息：</div>
						<div class="profile-info-value">
							<textarea id="description" name="description" style="resize:none;padding-left:3px !important;"
									  maxlength="200" rows="3" class="col-xs-10 col-sm-8"></textarea>
						</div>
					</div>
				</div>
			</div>
		</form>
	</div>
</div>


<script type="text/javascript">

	$(function(){
		loadUserGenderItems();
		loadUserTypeItems();
		loadUserEnabledItems();
		loadUserPositions();
		jQuery('#birthday').datetimepicker({
			format:'Y-m-d',
			timepicker:false,
			todayButton:true
		});
	});

	//从数据字典中加载用户性别
	function loadUserGenderItems(){
		$.ajax({
			type:'GET',
			url:'${ctx}/${frameworkPath}/item/USER_GENDER/sub',
			async:false,
			dataType:'json',
			success:function(data){
				var dataObj = eval("("+data+")");
				var tempAjax="";
				$.each(dataObj,function(idx,item){
					tempAjax += "<option value='"+item.value+"'>"+item.name+"</option>";
				})
				$("#gender").empty();
				$("#gender").append(tempAjax);
			},
			error: function(XmlHttpRequest, textStatus, errorThrown){
				lui.fail('加载数据失败！');
			}
		});
	}

	//从数据字典中加载用户类型
	function loadUserTypeItems(){
		$.ajax({
			type:'GET',
			url:'${ctx}/${frameworkPath}/item/USER_TYPE/sub',
			async:false,
			dataType:'json',
			success:function(data){
				var dataObj = eval("("+data+")");
				var tempAjax="";
				$.each(dataObj,function(idx,item){
					tempAjax += "<option value='"+item.value+"'>"+item.name+"</option>";
				})
				$("#userType").empty();
				$("#userType").append(tempAjax);
			},
			error: function(XmlHttpRequest, textStatus, errorThrown){
				lui.fail('加载数据失败！');
			}
		});
	}

	//从数据字典中加载是否启用items
	function loadUserEnabledItems(){
		$.ajax({
			type:'GET',
			url:'${ctx}/${frameworkPath}/item/COMMON_YESNO/sub',
			async:false,
			dataType:'json',
			success:function(data){
				var dataObj = eval("("+data+")");
				var tempAjax="";
				$.each(dataObj,function(idx,item){
					tempAjax += "<option value='"+item.value+"'>"+item.name+"</option>";
				})
				$("#enabled").empty();
				$("#enabled").append(tempAjax);
			},
			error: function(XmlHttpRequest, textStatus, errorThrown){
				lui.fail('加载数据失败！');
			}
		});
	}

	//加载用户岗位
	function loadUserPositions(){
		$.ajax({
			type:'GET',
			url:'${ctx}/${frameworkPath}/user/position?org=${param.org}',
			async:false,
			dataType:'json',
			success:function(data){
				var tempAjax="";
				$.each(data,function(idx,item){
					tempAjax += "<option value='"+item.ID+"'>"+item.NAME+"</option>";
				})
				$("#position").empty();
				$("#position").append(tempAjax);
			},
			error: function(XmlHttpRequest, textStatus, errorThrown){
				lui.fail('加载数据失败！');
			}
		});
	}

	var form = "user_form";
	var $form = $("#"+form);

	jQuery(function($) {
		//账号验证
		 jQuery.validator.addMethod("validateId", function (value, element) {
		 var zipcode = new RegExp("^[A-Za-z0-9]{1}[A-Za-z0-9_]{0,19}$");
		 return this.optional(element) || zipcode.test(value);
		 }, "输入一个有效的账号，账号不能以下划线开头，长度小于20位！");

		 //姓名验证
		 jQuery.validator.addMethod("validateName", function (value, element) {
		 var zipcode = new RegExp("^[A-Za-z0-9\u4e00-\u9fa5]{1}[A-Za-z0-9\u4e00-\u9fa5_]{0,19}$");
		 return this.optional(element) || zipcode.test(value);
		 }, "输入一个有效的姓名，不能以下划线开头，长度最多20位！");

		//生日验证
		jQuery.validator.addMethod("validateDate", function (value, element) {
			debugger;
			var zipcode = /^((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))$/;
			return this.optional(element) || zipcode.test(value);
		}, "输入有效的生日！");

		$form.validate({
			rules: {
				'name': {
					required: true,
					validateName:true
				},
				'order': {
					required: true
				},
				'username': {
					required: true,
					validateId:true
				},
				'email': {
					required: true,
					email:true
				},
				'birthday':{
					required:true
				}
			},

			messages: {
				'name':{
					required: "名称不能为空"
				},
				'order': {
					required: "排序字段不能为空"
				},
				'username': {
					required: "编号不能为空"
				}
			}
		});

	});

	function formValid(){
		var retObj =new Object();
		var valid = $("#"+form).valid();
		if (valid) {
			$form.ajaxSubmit({
				url : "${ctx}/${frameworkPath}/user/create?org=${param.org}",
				type : "post",
				async :false,
				dataType:'json',
				success : function(message) {
					if (message.success) {
						retObj.text ="保存成功！";
						retObj.state = true;
					} else {
						retObj.text = text;
						retObj.state =false;
					}
				}
			});
		} else {
			return;
		}
		return retObj;
	}
</script>
</body>
</html>